package com.monkeyboy.mytest;

public class MyTest {
    private static int c = 0;
    private static final Object o = new Object();

    public static void main(String[] args) {
        new Thread(new OddAndEvent(), "偶数").start();
        new Thread(new OddAndEvent(), "奇数").start();
    }


    static class OddAndEvent implements Runnable {
        @Override
        public void run() {
            while (c <= 100) {
                synchronized (o) {
                    System.out.println(Thread.currentThread().getName() + ":  " + c);
                    o.notify();
                    c++;
                    try {
                        if (c <= 100) {
                            o.wait();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}